package com.yunqiao.service.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.Query;
import com.yunqiao.service.api.BankCardService;
import com.yunqiao.service.api.entity.BankCardEntity;
import com.yunqiao.service.api.mapper.BankCardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("bankCardService")
public class BankCardServiceImpl extends ServiceImpl<BankCardMapper, BankCardEntity> implements BankCardService {

    @Autowired
    private BankCardMapper bankCardMapper;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //各种查询条件的过滤,在此进行
        QueryWrapper<BankCardEntity> queryWrapper = new QueryWrapper<>();
        if (params.get("userId") != null) {
            queryWrapper.eq("user_id", params.get("userId"));
        }
        IPage<BankCardEntity> page = this.page(new Query<BankCardEntity>(params).getPage(), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void saveBankCard(BankCardEntity bankCard) {
        Integer count = bankCardMapper.selectCount(new QueryWrapper<BankCardEntity>().eq("user_id", bankCard.getUserId()).eq("card_number", bankCard.getCardNumber()));
        if (count > 0) {
            throw new CustomApiException("这个银行卡已经绑定过了");
        }
        bankCardMapper.insert(bankCard);
    }

}
